﻿using System;
using System.Web.Caching;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using DAL;
using Common;
using System.Data.Linq;
namespace BAL
{
    public class Status
    {
        internal static StatusInfo TransStatus2StatusInfo(Models.Status level)
        {
            StatusInfo statusInfo = new StatusInfo();

            Utility.SetObjectValueWithTheSamePropertyName(statusInfo, level);

            return statusInfo;
        }

        public static StatusInfo AddStatus(StatusInfo statusInfo)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                Models.Status level = new Models.Status();
                Utility.SetObjectValueWithTheSamePropertyName(level, statusInfo);
                DatabaseHelper.Insert<Models.Status>(level);
                ToCache();
                return TransStatus2StatusInfo(level);
            }
        }

        public static void EditStatus(StatusInfo statusInfo)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                try
                {
                    Models.Status level = db.Status.Single(b => b.Id == statusInfo.Id);

                    Utility.SetObjectValueWithTheSamePropertyName(level, statusInfo);

                    db.SubmitChanges(ConflictMode.ContinueOnConflict);
                    ToCache();
                }
                catch (ChangeConflictException)
                {
                    foreach (ObjectChangeConflict occ in db.ChangeConflicts)
                    {
                        occ.Resolve(RefreshMode.KeepCurrentValues);
                    }
                }
            }
        }
        public void Update(int id, string name)
        {

        }
        public void Delete(int id)
        {

        }
        public static void DelStatus(int id)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                DelStatus(new int[] { id });
            }
        }

        public static void DelStatus(int[] lstId)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                if (lstId.Length > 0)
                {
                    string sql = string.Format("delete from Status where id in ({0})", string.Join(",", lstId.ConvertToStringArray()));
                    db.ExecuteCommand(sql);
                    ToCache();
                }
            }
        }

        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <returns></returns>
        public static void ToCache()
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                CacheHelper.Refurbish("AllStatus", db.Status.ToList(), 0, 60);
            }
        }

        public static StatusInfo GetStatusInfoById(int Id)
        {
            if (!CacheHelper.IsExist("AllStatus")) { ToCache(); }
            IList<Models.Status> statusInfo = (IList<Models.Status>)CacheHelper.GetCache("AllStatus");
            return TransStatus2StatusInfo(statusInfo.Single(b => b.Id == Id));
        }

        public static IList<StatusInfo> GetStatusInfos(int first, int max)
        {
            if (!CacheHelper.IsExist("AllStatus")) { ToCache(); }
            IList<Models.Status> statusInfo = (IList<Models.Status>)CacheHelper.GetCache("AllStatus");
            if (max > 0)
            {
                return statusInfo.Skip(first).Take(max).Select(t => TransStatus2StatusInfo(t)).ToList();
            }
            else
            {
                return statusInfo.Select(t => TransStatus2StatusInfo(t)).ToList();
            }
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    if (max > 0)
            //    {
            //        return db.GetTable<Models.Status>().Skip(first).Take(max).Select(c => TransStatus2StatusInfo(c)).ToList();
            //    }
            //    else
            //    {
            //        return db.GetTable<Models.Status>().Select(c => TransStatus2StatusInfo(c)).ToList();
            //    }
            //}
        }

        public static int GetStatusInfoCount()
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.GetTable<Models.Status>().Count();
            }
        }
        public static bool CheckStatusExistById(int Id)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.Status.Where(w => w.Id == Id).Count() > 0;
            }
        }

        public static bool CheckStatusExistByName(string name)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.Status.Where(w => w.Name == name).Count() > 0;
            }
        }

        public static IDictionary<int, string> GetDic()
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                //List<Status> list = new List<Status>();
                IList<Models.Status> list = new List<Models.Status>();
                list = db.GetTable<Models.Status>().ToList<Models.Status>();
                IDictionary<int, string> dictionary = new Dictionary<int, string>();
                foreach (Models.Status status in list)
                {
                    dictionary.Add(new KeyValuePair<int, string>(status.Id, status.Name));
                }
                return dictionary;
            }
        }

 

 

    }
}
